//
//  OutSickerViewController.m
//  HonlivApp
//
//  Created by admin on 2017/6/25.
//  Copyright © 2017年 honliv. All rights reserved.
//

#import "OutSickerViewController.h"
#import "WSDatePickerView.h"
#import "SickInfoCell.h"
#import "SickInfoFrame.h"
#import "SickInfoModel.h"
#import "OutSickerViewModel.h"

@interface OutSickerViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIButton *dateBtn;
@property (nonatomic,strong) NSMutableArray *datasArray;
@property (nonatomic,assign) NSInteger pageNum;
@property (nonatomic,copy) NSString *sqlNo;
@property (nonatomic,strong) NSDictionary *dic;

@end

@implementation OutSickerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"门诊患者";
    [self initTableView];
    [_tableView.mj_header beginRefreshing];
}
-(void)createTopView{
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 45)];
    [self.view addSubview:topView];
    UILabel *titleLab = [Factory createLabelWithTitle:@"选择日期:" frame:CGRectMake(20, 0, 100, 45) fontSize:15];
    [topView addSubview:titleLab];
    UIButton *dateBtn = [Factory createButtonWithTitle:[NSDate stringOfToday] frame:CGRectMake((kScreenW-100)*0.5, 0, 100, 45) backgroundColor:nil fontSize:17 target:self selector:@selector(createDatePicker)];
    self.dateBtn = dateBtn;
    [topView addSubview:dateBtn];
}
-(void)initTableView{
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self createTopView];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, kScreenW, kScreenH-45) style:UITableViewStyleGrouped];
    _tableView.contentInset = UIEdgeInsetsMake(-30, 0, 0, 0);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.sectionHeaderHeight = 0;
    _tableView.sectionFooterHeight = 5;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    [self createRefresh];
}
//上拉加载。下拉刷新
-(void)createRefresh{
    _datasArray = [NSMutableArray array];
    _tableView.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        _pageNum = 1;
        [_datasArray removeAllObjects];
        [self getDatasArray];
    }];
    _tableView.mj_footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
        _pageNum ++;
        [self getDatasArray];
    }];
    
}
//加载数据
-(void)getDatasArray{

    NSString *date = self.dateBtn.titleLabel.text;
    OutSickerViewModel *viewModel = [[OutSickerViewModel alloc] initWithType:self.type date:date doctor:@"0025" department:@"010512" page:_pageNum];
    [viewModel getOutSickerInfoSuccess:^(NSArray *array) {
        [_datasArray addObjectsFromArray:array];
        if (_datasArray.count == 0) {
            [SVProgressHUD showErrorWithStatus:@"暂无更多数据..."];
            [_tableView.mj_footer endRefreshingWithNoMoreData];;
        }else if (array.count == 0) {
            [_tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self endRefersh];
            [_tableView reloadData];
        }
    }];
}
//创建日期选择器
-(void)createDatePicker{
    WSDatePickerView *datepicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowYearMonthDay CompleteBlock:^(NSDate *startDate) {
        NSString *date = [startDate stringWithFormat:YMD];
        [self.dateBtn setTitle:date forState:UIControlStateNormal];
        if(_datasArray.count > 0){
            [_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
        }
        [_tableView.mj_header beginRefreshing];
    }];
    datepicker.doneButtonColor = BAR_BGCOLOR;//确定按钮的颜色
    NSDate *maxDate = [[NSDate dateYesterday] dateWithFormatter:YMDHMS];
    datepicker.maxLimitDate = maxDate;//@"yyyy-MM-dd HH:mm"
    [datepicker show];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    SickInfoFrame *frameModel = _datasArray[indexPath.section];
    return frameModel.cellHeight;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _datasArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SickInfoCell *cell = [SickInfoCell cellWithTableView:tableView];
    SickInfoFrame *model = _datasArray[indexPath.section];
    cell.sickInfoF = model;
    return cell;
}


//结束刷新
-(void)endRefersh{
    [_tableView.mj_header endRefreshing];
    [_tableView.mj_footer endRefreshing];
}


@end
